<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <!-- 搜索表单 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="编号" prop="productSn">
            <el-input v-model="queryParams.productSn" placeholder="请输入类别编号" clearable size="small"
                      @keyup.enter.native="handleQuery"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="productName">
            <el-input v-model="queryParams.productName" placeholder="请输入类别名称" clearable size="small"
                      @keyup.enter.native="handleQuery"></el-input>
          </el-form-item>
          <el-form-item label="类别" prop="categoryId">
            <el-select v-model="queryParams.categoryId" placeholder="请选择">
              <el-option v-for="item in categoryOptions"
                         :key="item.categoryId"
                         :value="item.categoryId"
                         @keyup.enter.native="handleQuery"
                         :label="item.categoryName">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 数据操作按钮 -->
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate">修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="small" :disabled="single" @click="handleDelete">删除
            </el-button>
          </el-col>
        </el-row>

        <!-- 数据展示Table -->
        <el-table :data="productList" style="width: 100%" v-loading="loading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"></el-table-column>
          <el-table-column prop="productId" label="ID" align="center" width="150"></el-table-column>
          <el-table-column prop="productSn" label="产品编号" align="center"></el-table-column>
          <el-table-column prop="productName" label="产品名称" align="center"></el-table-column>
          <el-table-column prop="price" label="价格" align="center"></el-table-column>
          <el-table-column prop="category.categoryName" label="归属类别" align="center"></el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页加载 -->
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryParams.pageNum"
                       :page-size="queryParams.pageSize"
                       :page-sizes="[5,10,20,50]" layout="total,sizes,prev,pager,next,jumper" :total="total">
        </el-pagination>

      </el-col>
    </el-row>

    <!-- 新增/修改对话框 默认不显示 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="productSn">
          <el-input v-model="form.productSn" placeholder="请输入编号"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择">
            <el-option v-for="item in categoryOptions" :key="item.categoryId" :label="item.categoryName"
                       :value="item.categoryId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listProductByPage, deleteProduct, getProduct, updateProduct, addProduct} from "@/api/product";
import {categoryList} from "@/api/category";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 下拉框选项数据
      categoryOptions: [],
      productList: [],
      // 查询参数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productSn: null,
        productName: null,
        categoryId: null
      },

      // 多选 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 新增，修改对话框
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单
      form: {},
      // 表单校验
      rules: {
        productSn: [
          {required: true, message: "编号不能为空", trigger: "blur"},
        ],
        productName: [
          {required: true, message: "名称不能为空", trigger: "blur"},
        ],
      },
    }
  },
  created() {
    this.getQueryList();
    this.getCategoryOptions();
  },
  methods: {
    /** 获取product的列表 */
    // getProductList() {
    //   productList().then(response => {
    //     this.productList = response.data;
    //   })
    // },
    getQueryList() {
      this.loading = true;
      listProductByPage(this.queryParams).then(response => {
        console.log(response.data)
        this.productList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      })
    },
    /** 查询category的下拉选项 */
    getCategoryOptions() {
      categoryList().then(response => {
        this.categoryOptions = response.data;
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      const productId = row.productId || this.ids;
      getProduct(productId).then(response => {
        console.log(response)
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productId = row.productId || this.ids;
      this.$confirm('是否确认删除ID为' + productId + '的数据项？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteProduct(productId);
      }).then(() => {
        this.getQueryList(); // 刷新数据
        this.$message.success("删除成功")
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getQueryList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.getQueryList();
    },
    /* 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length
    },
    /* 重置表单 */
    resetForm() {
      // 可进行数据初始化工作
      this.form = {
        productId: null,
        productSn: null,
        productName: null,
        price: null,
        categoryId: null
      }
    },
    /* 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "添加数据";
    },
    /* 修改product提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form.categoryId)
          if (this.form.productId !== null) {
            updateProduct(this.form).then(response => {
              console.log(response)
              this.$message.success(response.msg);
              this.getQueryList();
              this.cancel();
            })
          } else {
            addProduct(this.form).then(response => {
              this.$message.success(response.msg);
              this.getQueryList();
              this.cancel();
            })
          }
        }
      })
    },
    // form的取消按钮
    cancel() {
      this.open = false;
      this.title = '';
      this.resetForm()
    },
    // 分页方法
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getQueryList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getQueryList();
    },
    // 隐藏下拉框
    disableVisible() {
      this.$refs.loanStatusSelect.visible = false
    }
  }
}
</script>

<style scoped>

</style>